﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcMovie.Util;
using MvcMovie.Repositories;
using MvcMovie.Models;
using MvcMovie.Filters;

namespace MvcMovie.Controllers
{
    public class HelloWorldController : Controller
    {
        //GET: /HelloWorld/
        public ActionResult Index()
        {
            var movies = new MovieRepository().GetAll();
            return View(movies);
        }

        
        //GET: /HelloWorld/Create
        [Authorize]
        public ActionResult Create()
        {
            return View();
        }

        //POST: /HelloWorld/Create
        [HttpPost]
        [Authorize]
        [Log(Message = "Create Movie")]
        public ActionResult Create(Movie movie)
        {
            if (!ModelState.IsValid)
            {
                return View();
            }

            new MovieRepository().AddMovie(movie);
            TempData["mensagem"] = "The movie " + movie.Title + " was added successfully!";
            return RedirectToAction("Index");
        }

        //GET: /HelloWorld/Edition
        [Authorize]
        public ActionResult Edition(int id)
        {
            var movie = new MovieRepository().GetById(id);
            return View(movie);
        }

        //POST: /HelloWorld/Editon
        [HttpPost]
        [Authorize]
        [Log(Message = "Edition Movie")]
        public ActionResult Edition(Movie movie)
        {
            new MovieRepository().UpdateMovie(movie);
            TempData["menssagem"] = "The movie's edition was successfull!";
            return RedirectToAction("Index");
        }

        [Authorize]
        [Log(Message = "Deletion Movie")]
        public ActionResult Delete(int id)
        {
            var repository = new MovieRepository();
            repository.Delete(repository.GetById(id));
            TempData["mensagem"] = "The movie deletion was successful!";

            return RedirectToAction("Index");
        }
    }
}
